package com.backstage.util;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Vo/ENTITY双向转换工具类
 */
public class PojoUtil {
    //对象拷贝 ，将一个对象的同名属性拷贝到另外一个对象
    public static <T, S> T copyObject(S source, Class<T> tClass) {
        try {
            T target = tClass.newInstance();
            BeanUtils.copyProperties(source, target);
            return target;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //开发一时爽，维护火葬场
//将源列表的属性copy到目表列表上
    public static <T, S> List<T> copyList(List<S> sourceList, Class<T> tClass) {
        return sourceList.stream().map(entity -> copyObject(entity, tClass)).collect(Collectors.toList());
    }
}
